package com.boatAnchorAssistant.service.listener;

import com.boatAnchorAssistant.pojo.dto.department.CreateTopDepartmentDto;
import com.boatAnchorAssistant.pojo.dto.department.EditTopDepartmentDto;
import com.boatAnchorAssistant.pojo.event.tenant.CreateTenantEvent;
import com.boatAnchorAssistant.pojo.event.tenant.DeleteTenantEvent;
import com.boatAnchorAssistant.pojo.event.tenant.EditTenantEvent;
import com.boatAnchorAssistant.service.service.in.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Joseph.Li
 * @author joseph.li@king-v.com
 * Created in 2022-06-26 16:42
 * <p>
 * 部门模块监听者
 */
@Service
public class DepartmentListener {

    private final DepartmentService departmentService;

    public DepartmentListener(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    /**
     * 监听创建企业事件
     * <p>
     * 创建企业后需要创建其顶级部门
     *
     * @param event
     */
    @EventListener(CreateTenantEvent.class)
    public void departmentListener(CreateTenantEvent event) {
        CreateTopDepartmentDto dto = new CreateTopDepartmentDto ();
        BeanUtils.copyProperties (event, dto);
        dto.setName (event.getTenantName ());
        departmentService.createTopDepartment (dto);

    }


    /**
     * 监听编辑企业事件
     * <p>
     * 如果企业名称发生变化，修改顶级部门名称
     *
     * @param event
     */
    @EventListener(EditTenantEvent.class)
    public void departmentListener(EditTenantEvent event) {
        EditTopDepartmentDto dto = new EditTopDepartmentDto ();
        BeanUtils.copyProperties (event, dto);
        departmentService.editTopDepartment (dto);
    }


    /**
     * 监听删除企业事件
     * <p>
     * 批量删除企业下的部门
     *
     * @param event
     */
    @EventListener(DeleteTenantEvent.class)
    public void departmentListener(DeleteTenantEvent event) {
        List<Long> departmentIdList = event.getDepartmentIdList();
        departmentService.bathRemoveDepartment(departmentIdList);
    }


}
